package com.yls.except.controller;


import com.yls.except.service.ShopService;
import com.yls.except.util.GeneralResult;
import com.yls.except.util.IpAddressUitls;
import com.yls.except.util.StringIsempty;
import com.yls.except.util.exception.ExceptionHandle;
import org.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Generated;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 商家商品controller
 * Created by Administrator on 2016/8/4.
 */
@Controller
@RequestMapping(value = "shopcontroller")
public class ShopController extends ExceptionHandle {
    @Resource(name = "shopService")
    private ShopService shopService;

    /**
     * 获取首页故事信息
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "homepagestory.do")
    public void homePageStory(HttpServletRequest req, HttpServletResponse resp) {
        String page = req.getParameter("page");
        String count = req.getParameter("count");
        String type = req.getParameter("type");
        List<Object> list = shopService.selectPageHome(page, count, type);
        GeneralResult.resultList(list, resp);
    }

    /**
     * 获取首页banner
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "homepagebanner.do")
    public void homePageBanner(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = shopService.selectHomePageBanner();
        GeneralResult.resultList(list, resp);
    }

    /**
     * 获取良品banner
     * @param req
     * @param resp
     */
    @RequestMapping(value = "ichibanbanner.do")
    public void ichibanBanner(HttpServletRequest req, HttpServletResponse resp){
        List<Object> list = shopService.selectIchibanBanner();
        GeneralResult.resultList(list, resp);
    }

    /**
     * 获取故事详情
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "storyinfo.do")
    public void storyinfo(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String storyid = req.getParameter("storyid");
        String uid = req.getAttribute("uid") + "";
        if (StringIsempty.isallempty(storyid))
            GeneralResult.general(resp, list, 20001, 0);
        else {
            Map<String, Object> storyinfo = shopService.selectStoryInfoById(storyid, uid);
            GeneralResult.generalMap(resp, storyinfo);
        }
    }

    /**
     * 获取故事评价信息
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "storyevaluate.do")
    public void storyEvaluate(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String page = req.getParameter("page");
        String count = req.getParameter("count");
        String storyid = req.getParameter("storyid");
        String uid = req.getAttribute("uid") + "";
        if (StringIsempty.isallempty(storyid))
            GeneralResult.general(resp, list, 20001, 0);
        else {
            list = shopService.selectEvaluate(page, count, uid, storyid);
            GeneralResult.resultList(list, resp);
        }
    }

    /**
     * 故事评价
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "evaluate.action")
    public void evaluate(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String storyid = req.getParameter("storyid");
        String text = req.getParameter("text");
        String reqip = IpAddressUitls.getIpAddr(req);
        if (StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else if (StringIsempty.isallempty(storyid) || StringIsempty.isallempty(text))
            GeneralResult.general(resp, list, 20001, 0);
        else {
            int tag = shopService.insertStoryEvaluate(uid, storyid, text, reqip);
            GeneralResult.general(resp, list, tag, 1);
        }
    }

    /**
     * 故事评价点赞
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "praise.action")
    public void praise(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String evaluateid = req.getParameter("evaluateid");
        String reqip = IpAddressUitls.getIpAddr(req);
        if (StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else if (StringIsempty.isallempty(evaluateid))
            GeneralResult.general(resp, list, 20001, 0);
        else {
            int tag = shopService.insertStoryEvaluatePraise(uid, evaluateid, reqip);
            GeneralResult.general(resp, list, tag, 1);
        }
    }

    /**
     * 获取商品详情
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "shopinfo.do")
    public void shopinfo(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String shopid = req.getParameter("shopid");
        String uid = req.getAttribute("uid") + "";
        if (StringIsempty.isallempty(shopid))
            GeneralResult.general(resp, list, 20001, 0);
        else {
            Map<String, Object> map = shopService.selectShopInfoById(shopid, uid);
            GeneralResult.generalMap(resp, map);
        }
    }

    /**
     * 加入购物车
     *
     * @param req
     * @param resp
     */
    @RequestMapping(value = "addcart.action")
    public void addcart(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String shopid = req.getParameter("shopid");
        String num = req.getParameter("num");
        String bady_sex = req.getParameter("sex");
        String bady_age_id = req.getParameter("ageid");
        String shop_spec_price = req.getParameter("shopspecpriceid");
        String reqip = IpAddressUitls.getIpAddr(req);
        if(StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else if(StringIsempty.isallempty(shopid) || StringIsempty.isallempty(num) || StringIsempty.isallempty(bady_sex) || StringIsempty.isallempty(bady_age_id) || StringIsempty.isallempty(shop_spec_price))
            GeneralResult.general(resp, list, 20001, 0);
        else {
            int tag = shopService.insertShopCart(uid, shopid, num, bady_sex, bady_age_id, shop_spec_price, reqip);
            GeneralResult.general(resp, list, tag, 1);
        }
    }

    /**
     * 获取购物车信息
     * @param req
     * @param resp
     */
    @RequestMapping(value = "shopcart.action")
    public void getShopCart(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        if(StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else {
            Map<String, Object> map = shopService.selectShopCartByUid(uid);
            GeneralResult.generalMap(resp, map);
        }
    }

    /**
     * 修改购物车信息
     * @param req
     * @param resp
     */
    @RequestMapping(value = "editshopcart.action")
    public void editShopCart(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String cartinfo = req.getParameter("cartinfo");
        if(StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else if(StringIsempty.isallempty(cartinfo))
            GeneralResult.general(resp, list, 20001, 0);
        else {
            int tag = shopService.updateShopCart(uid, cartinfo);
            GeneralResult.general(resp, list, tag, 1);
        }
    }

    /**
     * 删除购物车信息
     * @param req
     * @param resp
     */
    @RequestMapping(value = "removeshopcart.action")
    public void removeShopCart(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String json = req.getParameter("json");
        Map<String, Object> map = GeneralResult.jsonStrToMap(json);
        if(StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else if(StringIsempty.isNullMap(map))
            GeneralResult.general(resp, list, 20001, 0);
        else {
            JSONArray cartinid = (JSONArray) map.get("cartinfo");
            int tag = shopService.deleteShopCart(uid, cartinid);
            GeneralResult.general(resp, list, tag, 1);
        }
    }

    /**
     * 我的收藏
     * @param req
     * @param resp
     */
    @RequestMapping(value = "mycollect.action")
    public void mycollect(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String page = req.getParameter("page");
        if(StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else {
            list = shopService.selectMyCollect(uid, page);
            GeneralResult.resultList(list, resp);
        }
    }

    /**
     * 删除我的收藏
     * @param req
     * @param resp
     */
    @RequestMapping(value = "delmycollect.action")
    public void delMycollect(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String story_id = req.getParameter("storyid");
        if(StringIsempty.isallempty(uid))
            GeneralResult.general(resp, list, 10001, 0);
        else if(StringIsempty.isallempty(story_id))
            GeneralResult.general(resp, list, 20001, 0);
        else {
            int tag = shopService.deleteMyCollect(uid, story_id);
            GeneralResult.general(resp, list, tag ,1);
        }
    }

    /**
     * 获取商品列表信息
     * @param req
     * @param resp
     */
    @RequestMapping(value = "shoplist.do")
    public void shoplist(HttpServletRequest req, HttpServletResponse resp) {
        String page = req.getParameter("page") + "";
        String uid = req.getAttribute("uid") + "";
        Map<String, Object> map = shopService.selectShopListInfo(page, uid);
        GeneralResult.generalMap(resp, map);
    }

    /**
     * 获取商品列表价目信息
     * @param req
     * @param resp
     */
    @RequestMapping(value = "shoppricelist.do")
    public void shopPriceListInfo(HttpServletRequest req, HttpServletResponse resp) {
        Map<String, Object> map = shopService.selectShopPriceListInfo();
        GeneralResult.generalMap(resp, map);
    }
}
